<?php
/**
 * 全局公共函数
 */

/**
 * 数据加密
 */
function apiEncrypt($data)
{
    return openssl_encrypt(json_encode($data), 'aes-256-cbc', 'secret', 0, env('API_ENCRYPT_IV'));
}
/**
 * 数据解密
 */
function apiDecrypt($encrypt)
{
    return json_decode(openssl_decrypt($encrypt, 'aes-256-cbc', 'secret', 0, env('API_ENCRYPT_IV')),true);
}
/**
 * 生成签名
 */
function createSign($timestamp,$nonce)
{
    $token = env('API_TOKEN');
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    return sha1( $tmpStr );
}
/**
 * curl_post请求
 */
function curlPost($url,$data=[])
{
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
/**
 * 返回数据
 */
function responseJson($code,$msg='',$data=[])
{
    if (is_array($code)) {
        echo json_encode($code,JSON_UNESCAPED_UNICODE);die;
    }
    echo json_encode(['code'=>$code,',msg'=>$msg,'data'=>$data],JSON_UNESCAPED_UNICODE);die;

}
